package com.czh.lg61homework;

import org.springframework.amqp.core.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class HomeworkApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(HomeworkApplication.class, args);
        AmqpAdmin amqpAdmin = context.getBean(AmqpAdmin.class);
        Queue dlxQueue = new Queue("i.m.dlx", false);
        amqpAdmin.declareQueue(dlxQueue);
        DirectExchange dlxDirectExchange = new DirectExchange("i.m.dlx.ex", false, false);
        amqpAdmin.declareExchange(dlxDirectExchange);
        amqpAdmin.declareBinding(BindingBuilder.bind(dlxQueue).to(dlxDirectExchange).withQueueName());

        Map<String, Object> props = new HashMap<>();
        props.put("x-dead-letter-exchange", "i.m.dlx.ex");
        props.put("x-dead-letter-routing-key", "i.m.dlx");
        Queue orderQueue = new Queue("order.q", false, false, false, props);
        amqpAdmin.declareQueue(orderQueue);
        DirectExchange orderDirectExchange = new DirectExchange("order.q.ex", false, false);
        amqpAdmin.declareExchange(orderDirectExchange);
        amqpAdmin.declareBinding(BindingBuilder.bind(orderQueue).to(orderDirectExchange).withQueueName());

        AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration("3000");
        amqpTemplate.send("order.q.ex", "order.q",
                new Message("true".getBytes(StandardCharsets.UTF_8), messageProperties));

        amqpTemplate.send("order.q.ex", "order.q",
                new Message("false".getBytes(StandardCharsets.UTF_8), messageProperties));
    }
}
